﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class ReplyController : BaseController
    {
        [AjaxOrChildAction]
        public ActionResult feComment(int? NewsID, int? page)
        {
            int TotalRecord = 0;
            var ListReply = RsDataManager.GetList<ReplyFullInfo>("ReplyGetListByNewsId", new { NewsID = NewsID, Status = 1 }, page ?? 1, 7, ref TotalRecord);
            ViewBag.TotalRecord = TotalRecord;
            ViewBag.RouteValues = new { NewsID = NewsID, Status = 1 };
            return PartialView(ListReply);
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult feWriteComment(int? NewsID)
        {
            NewsID = NewsID ?? 0;
            return PartialView(new ReplyInfo() { NewsID = (int)NewsID });
        }
        [HttpPost, AjaxOrChildAction]
        public ActionResult feWriteComment(ReplyInfo info)
        {
            info.CreateDate = DateTime.Now;
            info.UserID = UserID;
            info.Status = 1;
            if (RsDataManager.Save("ReplySave", info) > 0)
            {
                int TotalRecord = 0;
                var ListReply = RsDataManager.GetList<ReplyFullInfo>("ReplyGetListByNewsId", new { NewsID = info.NewsID, Status = 1 }, 1, 7, ref TotalRecord);
                ViewBag.TotalRecord = TotalRecord;
                ViewBag.RouteValues = new { NewsID = info.NewsID, Status = 1 };
                ModelState.Clear();
                return PartialView("feComment", ListReply);
            }
            return AjaxMessage(Message.CreateError);
        }
        [AjaxOrChildAction]
        public MvcHtmlString feGenerateCommentLink(string action, int userId, int id, int newsId, System.Web.Mvc.Ajax.AjaxOptions ajaxOption = null)
        {
            if (userId == UserID || IsAdministrator || IsContentManager)
            {
                TagBuilder link = new TagBuilder("a");
                link.AddCssClass(action + "-comment");
                link.Attributes["href"] = "/reply/" + action + "/" + newsId + "/" + id;
                link.Attributes["title"] = action.ToLower();
                if (ajaxOption != null) link.MergeAttributes(ajaxOption.ToUnobtrusiveHtmlAttributes());
                return MvcHtmlString.Create(link.ToString());
            }
            return MvcHtmlString.Empty;
        }
        [AjaxOrChildAction]
        public ActionResult feDelete(int id, int newsId)
        {
            if (RsDataManager.Delete("ReplyDelete", "ReplyID", id))
            {
                return RedirectToAction("feComment", new { NewsId = newsId });
            }
            return AjaxMessage(Message.DeleteError);
        }
    }
}
